package com.monitor.util;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class SVGUtil {

    /**
     * 从SVG文件加载图标
     * @param path SVG文件路径
     * @param width 目标宽度
     * @param height 目标高度
     * @return 图标对象
     */
    public static ImageIcon loadSVGIcon(String path, int width, int height) {
        try {
            // 确保路径以/开头
            if (!path.startsWith("/")) {
                path = "/" + path;
                System.out.println("已修正SVG文件路径: " + path);
            }
            System.out.println("尝试加载SVG文件: " + path);
            // 从资源加载SVG
            InputStream svgStream = SVGUtil.class.getResourceAsStream(path);
            if (svgStream == null) {
                System.err.println("无法加载SVG文件: " + path);
                // 尝试使用不同的类加载器
                svgStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path.substring(1));
                if (svgStream == null) {
                    System.err.println("使用上下文类加载器仍无法加载SVG文件: " + path);
                    return createPlaceholderIcon(width, height);
                }
                System.out.println("使用上下文类加载器成功加载SVG文件: " + path);
            }
            
            // 创建PNG转码器
            PNGTranscoder transcoder = new PNGTranscoder();
            transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) width);
            transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) height);
            
            // 设置输入和输出
            TranscoderInput input = new TranscoderInput(svgStream);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            TranscoderOutput output = new TranscoderOutput(outputStream);
            
            // 执行转换
            transcoder.transcode(input, output);
            
            // 关闭流
            svgStream.close();
            outputStream.flush();
            outputStream.close();
            
            // 从输出流创建图像
            InputStream in = new ByteArrayInputStream(outputStream.toByteArray());
            BufferedImage image = ImageIO.read(in);
            in.close();
            
            return new ImageIcon(image);
        } catch (IOException | TranscoderException e) {
            e.printStackTrace();
            return createPlaceholderIcon(width, height);
        }
    }
    
    /**
     * 创建占位图标
     */
    private static ImageIcon createPlaceholderIcon(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.GRAY);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.WHITE);
        g2d.drawLine(0, 0, width, height);
        g2d.drawLine(0, height, width, 0);
        g2d.dispose();
        return new ImageIcon(image);
    }
}